﻿using SymulatorEthernetWPF.SimObjects;
using SymulatorEthernetWPF.SimObjects.Devices;
using SymulatorEthernetWPF.UserInterface.UIElements;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SymulatorEthernetWPF.UserInterface
{
    /// <summary>
    /// Interaction logic for AdressSelection.xaml
    /// </summary>
    public partial class AdressSelection : Window
    {
        public delegate void AdressDelegate(MacAdress adress);
        public event AdressDelegate AdressChanged;
        public AdressSelection(DeviceControl deviceControl, bool destinationSelection)
        {
            InitializeComponent();
            if (!destinationSelection)
            {
                TreeViewItemHost host = new TreeViewItemHost((String)deviceControl.deviceLabel.Content);
                host.ExpandSubtree();
                foreach (NetworkInterface it in deviceControl.interfaces)
                {
                    TreeViewItemDevice temp = new TreeViewItemDevice(it);
                    temp.MouseDoubleClick += okButton_Click;
                    host.Items.Add(temp);
                }
                DeviceSelection.Items.Add(host);
            }
            else
            {
                foreach(UIElement it in Simulation.simObjects)
                {
                    if(it is HostControl && it!=deviceControl)
                    {
                        TreeViewItemHost host = new TreeViewItemHost((String)((DeviceControl)it).deviceLabel.Content);
                        host.ExpandSubtree();
                        foreach (NetworkInterface it2 in ((DeviceControl)it).interfaces)
                        {
                            TreeViewItemDevice temp = new TreeViewItemDevice(it2);
                            temp.MouseDoubleClick += okButton_Click;
                            host.Items.Add(temp);
                        }
                        DeviceSelection.Items.Add(host);
                    }
                }
            }
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            AdressChanged(((TreeViewItemDevice)DeviceSelection.SelectedItem).networkInterface.Adress);
            this.Close();

        }

        private void DeviceSelection_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (DeviceSelection.SelectedItem is TreeViewItemHost)
                okButton.IsEnabled = false;
            else
                okButton.IsEnabled = true;
        }
    }
}
